<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sblam
 *
 * @author jblew
 */
App::import('Vendor', 'sblam/sblamtest');
Configure::load("antispam");

define('SBLAM_SPAM', 2);
define('SBLAM_PROBABLE_SPAM', 1);
define('SBLAM_ERROR', 0);
define('SBLAM_PROBABLE_HAM', -1);
define('SBLAM_HAM', -2);

class SblamComponent extends Object {

    private $autoCheck = true;
    private $checked = false;
    private $fields = null;
    private $spamStatus = 0;

    public function initialize(&$controller, $settings) {
        if (isset($settings['autoCheck'])) {
            $this->autoCheck = $settings['autoCheck'];
        }
        if (isset($settings['fields'])) {
            $this->fields = $settings['fields'];
        }
    }

    public function startup(&$controller) {
        if ($this->autoCheck) {
            $this->spamStatus = sblamtestpost($this->fields, Configure::read("Sblam.apikey"));
            $this->checked = true;
        }
    }

    public function beforeRender(&$controller) {
        $controller->helpers[] = "Sblam";
    }

    public function getSpamStatus() {
        if ($this->checked) {
            return $this->spamStatus;
        } else {
            trigger_error("Post wasn't checked!");
        }
    }

    public function checkPostRequest() {
        if ($this->checked) {
            return $this->spamStatus;
        } else {
            $this->spamStatus = sblamtestpost($this->fields, Configure::read("Sblam.apikey"));
            $this->checked = true;
            return $this->spamStatus;
        }
    }

    public function reportUrl() {
        return sblamreporturl();
    }

}
?>
